<?php
//用户逻辑层	
namespace app\common\Logic;
use think\Db;
use lib\Curl;
class UserLogic{
	//手机号码注册
	public function register($param){
		$val=validate('CommonV');
		if(!$val->scene('register')->check($param)) return rel(-1,$val->getError());	//验证短信验证码
		$userInfo=[
			'nickname'		=>$param['nickName'],
			'sex'			=>$param['gender'],
			'city_name'		=>$param['city'],
			'province_name'	=>$param['province'],
			'head_img'		=>$param['avatarUrl'],
			'phone'			=>$param['phone']
		];
		$rel = Db::name('users')->where('id',$param['uid'])->update($userInfo);
		if (!$rel) {
			return rel(-1,'绑定手机号失败');
		} else {
			return rel(1,'绑定手机号成功');
		}
	}
	//小程序授权登录
	public function login($param){
		$val=validate('CommonV');
		if(!$val->scene('login')->check($param)) return rel(-1,$val->getError());
		try{
			$rel=$this->get_user_openid($param['code']);
			$openid		=$rel['openid'];
			$session_key=$rel['session_key'];
			$user = Db::name('users')->where('openid_s',$openid)->find();
			if (!$user) {
				$insert=[
					'openid_s'	=> $openid,
					'session_key'=>$session_key,
					'ctime'		=> time()
				];
				$uid=Db::name('users')->insertGetId($insert);
				$rel=[
					'uid'		=> $uid,
					'type'		=> 1			//用户状态1：注册，2：登录
				];
				return rel(1,'新注册用户',$rel);
			} else {
				$rel=[
					'uid'	=> $user['id'],
					'type'  => 2
				];
				return rel(1,'登录成功',$rel);
			}
		}catch (\Exception $e){
			return rel(-1,$e->getMessage());
		}
	}
	//更新用户数据
	public function update_user($param){
		$val=validate('CommonV');
		if(!$val->scene('update_user')->check($param)) return rel(-1,$val->getError());	//验证短信验证码
			$userInfo=[
				'nickname'		=>$param['nickName'],
				'city_name'		=>$param['city'],
				'province_name'	=>$param['province'],
				'head_img'		=>$param['avatarUrl']
			];
			$rel = Db::name('users')->where('id',$param['uid'])->update($userInfo);
			if (!$rel) {
				return rel(-1,'更新用户数据失败');
			} else {
				return rel(1,'更新用户数据成功');
			}
	}
	//手机号+验证码登录
//	public function login2($param){
//		$param['sms_type']=1;
//		$val=validate('CommonV');
//		if(!$val->scene('login2')->check($param)) return rel(-1,$val->getError());
//		$user=Db::name('users')->where('phone',$param['phone'])->find();
//		if(!$user){
//			return rel(-1,'该手机号未注册');
//		}else{
//			return rel(1,'登录成功',$user);
//		}
//	}
	//根据临时code获取用户openID
	public function get_user_openid($code){
		$appid =config('base.miniprogram')['appid'];
		$secret=config('base.miniprogram')['secret'];
		$url="https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$code."&grant_type=authorization_code";
		$rel=Curl::https_curl($url);
		$rel=json_decode($rel,true);			//获取用户openID
		if(!empty($rel['openid'])){
			return $rel;
		}else{
			throw new \Exception('获取openID失败');
		}
	}
	//通过小程序加密数据获取明文数据
	protected function decryptData( $encryptedData,$session_key,$iv){
		$appid=config('base.miniprogram')['appid'];
		if (strlen($session_key) != 24) return rel(-41001,'encodingAesKey 非法');
		$aesKey=base64_decode($session_key);

		if (strlen($iv) != 24) return rel(-41002,'错误');
		$aesIV=base64_decode($iv);
		$aesCipher=base64_decode($encryptedData);
		$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
		$dataObj=json_decode( $result );
		if( $dataObj  == NULL ) return rel(-41003,'aes 解密失败');
		if( $dataObj->watermark->appid != $appid ) return rel(-41003,'aes 解密失败');
		$data = json_decode($result,1);
		return rel(1,'解析成功',$data);
	}
}